package com.bkhc.product.service;

import com.bkhc.common.entity.User;
import com.bkhc.common.entity.UserRole;
import com.bkhc.common.repository.UmcRepository;
import com.bkhc.common.repository.UserRepository;
import com.bkhc.common.repository.UserRoleRepository;
import com.bkhc.common.util.CommonUtil;
import com.bkhc.common.util.response.ResultCode;
import com.bkhc.common.util.response.ResultJson;
import com.bkhc.common.vo.UserBean;
import com.bkhc.product.common.RoleContant;
import com.bkhc.product.entity.*;
import com.bkhc.product.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ProductorService {

    @Autowired
    private UserProductRepository  repository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UmcRepository umcRepository;
    @Autowired
    private UserRoleRepository userRoleRepository ;

    //增加產品合夥人
    public ResultJson<?> addProductor(String userId, String productCode) {
        if(!StringUtils.isEmpty(productCode)){
            List<UserProduct> userProducts = repository.findByProductCode(productCode);
            if(!CollectionUtils.isEmpty(userProducts)){
                ResultJson<?> resultJson = ResultJson.failure(ResultCode.ISEXIT_RESURCE);
                return resultJson;
            }
        }

        User user = umcRepository.findByUserId(userId);

        UserRole userRoleOld = userRoleRepository.findByUserIdAndRoleCode(userId, RoleContant.PRODUCTOR);
        if(userRoleOld!=null){
            ResultJson<?> resultJson = ResultJson.failure(ResultCode.ISEXIT_RESURCE);
            return resultJson;
        }
        userRepository.save(user);
        UserRole userRole = new UserRole();
        userRole.setRoleCode(RoleContant.PRODUCTOR);
        userRole.setUserId(user.getUserId());
        userRole.setStatus("1");
        UserBean cureentUser = CommonUtil.getCrruentUser();
        userRole.setCreator(cureentUser.getUserId());
        userRole.setUpdator(cureentUser.getUserId());
        userRoleRepository.save(userRole);
        if(!StringUtils.isEmpty(productCode)){
            UserProduct userProduct = new UserProduct();
            userProduct.setProductCode(productCode);
            userProduct.setUserId(userId);
            repository.save(userProduct);
        }
        ResultJson<?> resultJson = ResultJson.success();
        return resultJson;
    }

    public ResultJson<?> transterProductor(String userId,String toUserId) {
        List<UserProduct> userProducts = repository.findByUserId(userId);
        UserBean u = CommonUtil.getCrruentUser();
        if(!CollectionUtils.isEmpty(userProducts)){
            for(UserProduct userProduct:userProducts){
                userProduct.setUserId(toUserId);
                userProduct.setUpdator(u.getUserId());
            }
        }
        repository.saveAll(userProducts);
        ResultJson<?> resultJson = ResultJson.success();
        return resultJson;
    }
}
